function codedx = dummycode(x)


%%%%%
% This function turns the categorical variable x into dummy-coded columns.
% 
% Input:
% x - 1D vector containing categorical data, each category should be indicated by an integer and consecutive integers should be used.
%
% Output:
% codedx - dummy-coded x where the number of columns equals the number of categories minus one.
%
% Written by Joon Lee, 2011
%%%%%


minx=min(x);
maxx=max(x);

codedx=zeros(length(x),maxx-minx);

for i=1:1:maxx-minx
    idx= x==minx+i;
    codedx(idx,i)=ones(sum(idx),1);
end

